<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/1/10
 * Time: 17:52
 */

namespace Home\Controller;


use Admin\Model\AssayClassModel;
use Admin\Model\AssayModel;
use Admin\Model\HealthRecordModel;
use Admin\Model\OperationModel;
use Admin\Model\OperationStateModel;
use Admin\Model\RecordPictureModel;
use Base\Controller\BaseHomeController;

class OperationController extends BaseHomeController
{
    protected $model;
    public function __construct()
    {
        parent::__construct(true);
        $this->model = new OperationModel();
    }

    /**
     * @param $id
     * 获取全部数据并注入模板
     */
   public function getAll($id){
       $list = $this->model->where(array('id'=>$id))->relation(true)->select();
       if(!empty($list)){
           $list[0]['link']['description'] = json_decode($list[0]['link']['description'],true);//检查结果后台保存位JSON数据
           $list[0]['link']['diagnosis'] = json_decode($list[0]['link']['diagnosis'],true);//检查项后台保存为JSON数据
           $list[0]['booking_date'] = json_decode($list[0]['booking_date'],true);//检查项后台保存为JSON数据
           //获取检测项、检测指标、检测内容
           $link = new AssayModel();
           $recordModel= new OperationModel();
           $row  = new OperationStateModel();
           $_GET['id'] = array('1','2','3','4');
           $status = $row->relation(true)->select();
           $res = $recordModel->relation(true)->select();
           $data =$res[0]['record_picture'];
           $row = $link->relation(true)->select();
           }
       $this->assign('status',$status);
       $this->assign('list',$list);
       $this->assign('res',$data);
       $this->assign('row',$row);
   }

    /**
     * @param $id
     * 编辑化验单
     */
    public function edit($id){
        $recordModel= new HealthRecordModel();
        $res = $recordModel->where(array('id'=>$id))->relation(true)->select();
        $row = $res[0]['record_picture'];
        $this->assign('row',$row);
        $this->show('');
    }

    /**
     * 化验单审核
     */
    public function checkSubmit(){
        //审核维通过
            if($_POST['optionsRadios'] == 0){
                $data = $this->model->select();
                $id = $data[0]['id'];
                //新增医生指导
                $assay = new AssayModel();
                $list = $assay->setField(array('guide'=>$_POST['doc_name']));
                $assay->save($list);
//                $list = $assay->setField(array('guide'=>$_POST['docName']));
//                $assay->add($list);
//                dump($list);exit();
//                if($list){
//                }
                //请为患者增加检查项
                $this->redirect('/Home/Operation/showAddLink',array('id'=>$id));
            }
            else{
                //审核通过
                $data = $this->model->where(array('id'=>$_POST['id']))->select();
                $id = $data[0]['id'];
                if($data){
                    //审核成功跳转
                    $status = new OperationStateModel();
                    $result['status'] = $status->relation()->where()->select();
                    if($result['status'] ==2){
                        $this->redirect('/Home/Operation/showList',array('id'=>$id,$_GET['id']=>2));
                    }
                    if($result['status'] ==3){
                        $this->redirect('/Home/Operation/showList',array('id'=>$id,$_GET['id']=>3));
                    }
                    if($result['status'] ==4){
                        $this->redirect('/Home/Operation/showList',array('id'=>$id,$_GET['id']=>4));
                    }
                    else{
                        $this->redirect('/Home/Operation/showList',array('id'=>$id));
                    }
                }
            }
    }
    /**
     *审核未通过 增加化验类型 提交
     * 我要手术、术前准备
     */
    public function addAssay(){
        $where['doctor_id']  = $_POST['doctor_id'];
        $where['patient_id'] = $_POST['patient_id'];
        $where['title']      = $_POST['options_values'];
        $where['content']    = $_POST['me_type'];

        $where['_logic']  ='and';
        $map['_complex'] = $where;
        //医生ID  患者ID    检查类型 检查时间
        if(empty($map)){
            $this->model->validate(false);
        }else{
            $link = new AssayModel();
            $data = $link->where($map)->relation(true)->create();
            if($data){
                $this->success();
            }else{
                $this->error($this->model->getError());
            }
        }
    }
    /**
     * 展示化验单
     */
    public  function showAddLink(){
        $recordModel= new RecordPictureModel();
        $list = $recordModel->select();
        $assay = new AssayModel();
        $result = $assay->select();
        $this->assign('res',$result);
        $this->assign('list',$list);
        $this->show('');
    }
    /**
     * 展示添加类型界面
     */
    public function showHydList(){
        $medical = new AssayClassModel();
        $list = $medical->relation(true)->select();
        $operation = new OperationModel();
        $row = $operation->relation(true)->select(array('status =1 and order asc'));
        $this->assign('list',$list);
        $this->assign('row',$row);
        $this->show('');
    }
//    /**
//     * 术前准备化验单审核
//     */
//    public function readyCheckSubmit(){
//        if($_POST['optionsRadios'] == 0) {
//            $data = $this->model->where(array('id'=>$_POST['id']))->select();
//            $id = $data[0]['id'];
//            //请为患者增加检查项
//            $this->redirect('/Home/Operation/showList',array('id'=>$id));
//            return false;
//        }
//        else{
//            $row = $this->model->where(array('id'=>$_POST['id']))->select();
//            if($row){
//                $result = $this->model->setField(array('guide'=>$_POST['docName']));
//                if($result){
//                    $this->model->save($row);
//                    //审核成功跳转预约手术
//                }
//                //else{
////                    $this->error($this->model->getError('操作失败'));
////                }
////            }else{
////                $this->error($this->model->getError('操作失败'));
//            }
//        }
//    }
    /**
     *我要手术提交
     */
//    public function operationSubmit(){
//
//        if(empty($_POST['info'])){
//            $this->model->validate(false);
//        }else{
//                $data = $this->model->where(array('id'=>$_POST['id']))->setField(array('status_id'=>1,'link_status_id'=>2));
//                if($data){
//                    $data['title'] = $_POST['info'];
//                    $data['title'] = json_encode($data['title'],true);
//                    $this->model->save($data);
//                    //如果全部通过跳转预约手术
//                }else{
//                    $this->error($this->model->getError());
//                }
//            }
//    }
    /**
     * 预约手术
     */
//    public function addDate(){
//        if(empty($_POST['booking_date'])){
//        $this->model->validate(false);
//        }else{
//            $this->model->save(array('id'=>$_POST['id'],'booking_date'=>$_POST['booking_date']));
//        }
//    }
    /**
     * 预约手术提交
     */
//    public function mySubmit(){
//    if(empty($_POST['info'])){
//        $this->model->validate(false);
//    }else{
//        $data = $this->model->where(array('id'=>$_POST['id']))->setField(array('status_id'=>2));
//        if($data){
//            $data['title'] = $_POST['info'];
//            $data['title'] = json_encode($data['title'],true);
//            $this->model->save($data);
//        }else{
//            $this->error($this->model->getError());
//        }
//    }
//}
    /**
     *术前准备提交
     */
//    public function readySubmit(){
//        if(empty($_POST['info'])){
//            $this->model->validate(false);
//        }else{
//            $data = $this->model->where(array('id'=>$_POST['id']))->setField(array('status_id'=>3));
//            if($data){
//                $data['title'] = $_POST['info'];
//                $data['title'] = json_encode($data['title'],true);
//                $this->model->save($data);
//                //
//            }else{
//                $this->error($this->model->getError());
//            }
//        }
//    }
    /**
     * 我在医院、我出院了审核
     */
//    public function homeCheckSubmit(){
//        if($_POST['optionsRadios'] == 0) {
//            $row = $this->model->where(array('id' => $_POST['id']))->select();
//            if ($row) {
//                $result = $this->model->setField(array('guide' => $_POST['docName']));
//                if ($result) {
//                    $this->model->save($result);
//                    $id = $row[0]['id'];
//                    //请为患者增加检查项
//                    $this->redirect('/Home/Operation/showList',array('id'=>$id));
//                    return false;
//                }
//            }
//        }else{
//            $row = $this->model->where(array('id'=>$_POST['id']))->select();
//            if($row){
//                $result = $this->model->setField(array('guide'=>$_POST['docName']));
//                if($result){
//                    $this->model->save($row);
//                }
//                //else{
////                    $this->error($this->model->getError('操作失败'));
////                }
////            }else{
////                $this->error($this->model->getError('操作失败'));
//            }
//        }
//    }
    /**
     * 我在医院提交
     */
//    public function hospitalSubmit(){
//        if(empty($_POST['info'])){
//            $this->model->validate(false);
//        }else{
//            $data = $this->model->where(array('id'=>$_POST['id']))->setField(array('status_id'=>4));
//            if($data){
//                $data['title'] = $_POST['info'];
//                $data['title'] = json_encode($data['title'],true);
//                $this->model->save($data);
//            }else{
//                $this->error($this->model->getError());
//            }
//        }
//    }
    /**
     * 我出院了提交
     */
//    public function homeSubmit(){
//        if(empty($_POST['info'])){
//            $this->model->validate(false);
//        }else{
//            $data = $this->model->where(array('id'=>$_POST['id']))->setField(array('status_id'=>5));
//            if($data){
//                $data['title'] = $_POST['info'];
//                $data['title'] = json_encode($data['title'],true);
//                $this->model->save($data);
//            }else{
//                $this->error($this->model->getError());
//            }
//        }
//    }

    /**
     * 我在医院
     */
//    public function addDay(){
//        if(empty($_POST['day']) || empty($_POST['info'])){
//            $this->model->validate();
//        }else{
//            $this->model->save(array('id'=>$_POST['id'],'booking_day'=>$_POST['day'],'title'=>$_POST['info']));
//        }
//    }
    /**
     *我出院了
     */
//    public function addMonth(){
//        if(empty($_POST['month'])|| empty($_POST['info'])){
//            $this->model->validate();
//        }else{
//            $this->model->save(array('id'=>$_POST['id'],'booking_month'=>$_POST['month'],'title'=>$_POST['info']));
//        }
//    }
    /**
     * @param $id
     * 展示模板
     */
    public function showList($id)
    {
        $this->getAll($id);
        $this->show('');
    }

//    public function getMedicalList(){
//        $where['status'] = 1;
//        $medicalModel= new \Admin\Model\MedicalModel();
//        $list = $medicalModel->relation(false)->field('id,title')->where($where)->select();
//        return $list;
//    }
    /**
     * @param string $medical_id
     * 根据检测目录获取检测指标的数据
     */
//    public function getMeTypeListJson($medical_id = ''){
//        $meTypeModel = new MeTypeModel();
//        $list = $meTypeModel->relation(false)->field("id,title")->where("status=1 and medical_id=%d",array($medical_id))->select();
//        $this->ajaxReturn($list);
//    }
    /**
     * @param string $me_type_id
     * 根据指标获取检测内容的数据
     */
//    public function getMeInfoListJson($me_type_id = ''){
//        $meInfoModel = new MeInfoModel();
//        $list = $meInfoModel->relation(false)->field("id,title")->where("status=1 and me_type_id=%d",array($me_type_id))->select();
//        $this->ajaxReturn($list);
//    }
}